package com.skywolf.chem.usercenter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.sysconfig.utils.TLBeanUtils;
import com.skywolf.chem.usercenter.entity.Permission;
import com.skywolf.chem.usercenter.mapper.PermissionMapper;
import com.skywolf.chem.usercenter.pojo.vo.PermissionVO;
import com.skywolf.chem.usercenter.service.IPermissionService;
import com.skywolf.chem.utils.PermissionTreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author
 * @since 2019-12-02
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<PermissionVO> listPermissionsTree() {
        List<Permission> permissionList = this.list();
        List<PermissionVO> permissionVOList = Collections.emptyList();
        if (permissionList.size() > 0) {
            permissionVOList = PermissionTreeUtils.buildTreeByRecursive(TLBeanUtils.copyList(permissionList, PermissionVO.class));
        }
        return permissionVOList;
    }

    @Override
    public List<PermissionVO> listUserPermissions(Long userId) {
        List<Permission> permissionList = permissionMapper.selectByUserId(userId);
        return TLBeanUtils.copyList(permissionList, PermissionVO.class);
    }
}
